<?php

namespace App\Http\Resources\Plane\PlaneOrder;

use App\Facades\Plane\PlaneCitySeedFacade;
use App\Facades\Plane\PlaneCompanySeedFacade;
use App\Models\Plane\Airports\Repositories\Interfaces\AirportRepositoryInterface;
use App\Trip\Plane\CarrierCode;
use Carbon\Carbon;
use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class PlaneOrderFlightResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array|Arrayable|JsonSerializable
     * @throws Exception
     */
    public function toArray($request)
    {

        $flight_info                          = parent::toArray($request);
        $flight_info['start_at']              = Carbon::parse($flight_info['start_at'])->format("Y-m-d H:i");
        $flight_info['arrive_at']             = Carbon::parse($flight_info['arrive_at'])->format("Y-m-d H:i");
        $flight_info['start_date']            = Carbon::parse($flight_info['start_at'])->format("Y-m-d");
        $flight_info['arrive_date']           = Carbon::parse($flight_info['arrive_at'])->format("Y-m-d");
        $flight_info['start_time']            = Carbon::parse($flight_info['start_at'])->format("H:i");
        $flight_info['arrive_time']           = Carbon::parse($flight_info['arrive_at'])->format("H:i");
        $flight_info['carrier_code']          = CarrierCode::getCarrierCode($flight_info['flight_no']);
        $flight_info['company_name']          = PlaneCompanySeedFacade::getCompanyNameByCode($flight_info['carrier_code']);
        $flight_info['company_name_uy']       = PlaneCompanySeedFacade::getCompanyNameUyByCode($flight_info['carrier_code']);
        $flight_info['company_short_name']    = PlaneCompanySeedFacade::getCompanyShortNameByCode($flight_info['carrier_code']);
        $flight_info['company_short_name_uy'] = PlaneCompanySeedFacade::getCompanyShortNameUyByCode($flight_info['carrier_code']);
        if (isset($flight_info['from_city_code'])) {
            $flight_info['from_city_name']    = PlaneCitySeedFacade::getCityNameByCode($flight_info['from_city_code']);
            $flight_info['from_city_name_uy'] = PlaneCitySeedFacade::getCityNameUyByCode($flight_info['from_city_code']);
            $flight_info['to_city_name']      = PlaneCitySeedFacade::getCityNameByCode($flight_info['to_city_code']);
            $flight_info['to_city_name_uy']   = PlaneCitySeedFacade::getCityNameUyByCode($flight_info['to_city_code']);
        } else {
            // 列表可能没有 需要重新查询
            $repo                             = app(AirportRepositoryInterface::class);
            $from_city_code                   = $repo->findOneByCode($flight_info['from_code'])?->getPlaneCityCode() ?: '';
            $to_city_code                     = $repo->findOneByCode($flight_info['to_code'])?->getPlaneCityCode() ?: '';
            $flight_info['from_city_name']    = PlaneCitySeedFacade::getCityNameByCode($from_city_code);
            $flight_info['from_city_name_uy'] = PlaneCitySeedFacade::getCityNameUyByCode($from_city_code);
            $flight_info['to_city_name']      = PlaneCitySeedFacade::getCityNameByCode($to_city_code);
            $flight_info['to_city_name_uy']   = PlaneCitySeedFacade::getCityNameUyByCode($to_city_code);
        }
        if (isset($flight_info['cabin_name'])) {
            $flight_info['cabin_name_uy'] = trans2uy($flight_info['cabin_name']);
        }
        unset(
            $flight_info['id'],
            $flight_info['user_id'],
            $flight_info['order_sn'],
            $flight_info['created_at'],
            $flight_info['updated_at'],
        );
        return $flight_info;
    }
}
